﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta><style>/*<![CDATA[*/

table{border: 1px solid gray;}
td{border: 1px dotted gray;}
p{margin: 3px 0 3px 0; padding: 0;}
#ID_Footer{font-size: small; font-style: italic; text-align: right; margin-top: 4em; padding-top: 4px; border-top: 2px solid gray;}

/*]]>*/</style><title>21. 22. ThinkPHP 3.1.2 控制器的模块和操作 1 2</title></head><body>
<div><br /></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 14pt; font-weight: bold; line-height: 190%">本节课大纲：</span></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; line-height: 190%">一、空模块和空操作</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%"> &nbsp; &nbsp; &nbsp; &nbsp;1. 空操作</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;function </span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #ff0000; line-height: 140%">_empty</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%">(</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #0000ff; line-height: 140%">$name</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #0000ff; line-height: 140%">)</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%">{</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">show</span><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%">(&quot;$name 不存在 &lt;a href=&apos;__APP__/Index/index&apos;&gt;返回首页&lt;/a&gt;&quot;);</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 240%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;2. 空模块</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;class </span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #ff0000; line-height: 140%">EmptyAction </span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%">extends Action{</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;function index(){</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//只需在此处写上想要输出的代码即可，下面几行是兄弟连计师演示显示已有模板页面所加</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$city=M(&apos;City&apos;);</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$arr=$city-&gt;select();</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;assign(&apos;list&apos;,$arr);</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$name=</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #0000ff; line-height: 140%">MODULE_NAME;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #ff0000; line-height: 140%">display</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%">(&quot;City:$name&quot;);</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;}</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; line-height: 140%">二、</span><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; line-height: 140%">前置操作和后置操作</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;1、前置操作： </span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #ff0000; line-height: 140%">_before_操作名</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//方法(操作)执行前</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;2、后置操作： </span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #ff0000; line-height: 140%">_after_操作名</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//方法执行后</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%">++++++++++++++++++++Leo+++++++++++++++++++++++++</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 190%">__APP__ </span><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%">: &nbsp;一直到模块的路径，不包含模块(即一直到......./Tpl/）</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%">一定要设置表前缀，没有设置的话，默认会为</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #ff0000; line-height: 140%">think_</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%">，没有前缀的话设置为&apos;&apos;;</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%">本课项目步骤：</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;1. 新建一个新项目thinkphp3</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;2. 创建一个新的数据库thinkphp3, utf8_general_ci字符集</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;新建一个user表，3个字段，id: INT , username VARCHAR 长度30, password CHAR 长度32</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;存储引擎MyISAM, 整理（校对字符集为）utf8_general_ci</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;新建一个城市表，3个字段，id: INT, name VARCHAR 30, title texts</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;存储引擎MyISAM, 整理utf8_general_ci</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;3. 向表中插入数据 &nbsp; &nbsp; &nbsp; &nbsp;</span></div>
<div><br /></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">配置项：</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;&apos;</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #ff0000; line-height: 140%">DB_DSN</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%">&apos;=&gt;&apos;mysql://root:@localhost:3306/thinkphp3&apos;,</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">css</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;#daohang{</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;height:150px;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;background: #abcedfg;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;a {</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;border: 1px solid gray;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-size: 60px;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;text-decoration: none; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//去掉超链接的下划线</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 190%">公共模板</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%"> &nbsp; &nbsp; &nbsp; &nbsp;放在，自建的.../Tpl/Public</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;其它模板中引入: &lt;</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #ff0000; line-height: 140%">include file</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%">=&quot;Public: header&quot;);</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">foreach</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;&lt;foreach </span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">name</span><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%">=&apos;list&apos; </span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">item</span><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%">=&apos;v&apos;&gt;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;a href=&apos;__APP__/city/{$v.name}&apos;&gt;{$v.name}&lt;/a&gt;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;&lt;/foreach&gt;</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">空方法/空操作</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;当访问一个不存在的方法时，自动调用空方法</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;function _empty($name){ &nbsp; &nbsp; &nbsp; &nbsp;//传递进来的是空方法名$name</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;...</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;}</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">show</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;用 $this-&gt;show(...)来输出，而不用echo</span></div>
<div><br /></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">空模块</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;新建一个空模块：</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">EmptyAction.class.php</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;class EmptyAction extends Action{</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public function index(){</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;show(&apos;&lt;p&gt;该方法不存在&lt;/p&gt;&apos;;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp;</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$name = MODULE_NAME;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;Display(&apos;City:$name&apos;); &nbsp; &nbsp; &nbsp; &nbsp;//访问City下的$name模板</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%">常量：</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #ff0000; line-height: 140%">MODULE_NMAE</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%">, 当前模块名称</span></div>
<div><br /></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%">访问主页时，没有登陆，则让其登陆</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;在_before_index里面判断是否登陆（属于IndexAction类的方法） &nbsp; &nbsp; &nbsp; &nbsp;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//_SESSION[&apos;username&apos;] ????</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(!isset(_SESSION[&apos;username&apos;]) || (_SESSION[&apos;username&apos;] ==&quot;&quot;){</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;redirect(&apos;Login/index&apos;); &nbsp; &nbsp; &nbsp; &nbsp;//跳转到Login模块下的index方法</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;登陆之后，向_SESSION[&apos;username&apos;]里面写值</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;使用_SESSION要首先开始session start?</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;thinkphp已经自动默认开启</span></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div><script type="text/javascript" language="javascript" src="jquery.js"></script><script type="text/javascript" language="javascript" src="itemlink.js"></script></body></html>